﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RayDen.Library.Core.Primitives;
using RayDen.Library.Data.Imaging;

namespace RayDen.Library.Components.Surface.Spectral
{
    public static class SpectralData
    {
        public static double[] EmeraldAbsorption =
        {
            380, 0.184058,
            400, 0.199834,
            420, 0.220868,
            440, 0.173539,
            460, 0.141987,
            480, 0.078881,
            500, 0.036812,
            520, 0.039441,
            540, 0.068363,
            560, 0.105175,
            580, 0.147246,
            600, 0.184058,
            620, 0.231386,
            640, 0.262939,
            660, 0.241904,
            680, 0.236644,
            700, 0.13147,
            720, 0.105175,
            740, 0.089398,
            760, 0
        };

        private static RegularSPD spd =
            new RegularSPD(EmeraldAbsorption.Where(item => item < 1).Select(item => (float) item).ToArray(), 380, 760,
                20);


        public static SampledSpectrum EmeraldAbsorbtion
        {
            get
            {
                return
                    ColorFactory.CreateSampled(EmeraldAbsorption.Where(item => item < 1).Select(item => (float) item).ToArray());
            }
        }

    }


    public class GemstoneSpectralData
    {
        public SpectralDistribution Absorbance;
        public SpectralDistribution Reflectivity;
        public SpectralDistribution IndexOfRefraction;

        public double   BirefringenceFactor;

        public float IorMin, IorMax;
        public bool DoubleRefraction;

    }


    public static class Gemstones
    {
        
        public static GemstoneSpectralData Emerald = new GemstoneSpectralData()
        {
            Absorbance = new RegularSPD(SpectralData.EmeraldAbsorption.Select(item=>(float)item).ToArray(),380,760,20 ),
            BirefringenceFactor = 0.006,
            DoubleRefraction = true,
            IorMin = 1.565f,
            IorMax = 1.602f,
            IndexOfRefraction = ComputeIor(1.565f, 1.602f, 380, 760, 20f),
            Reflectivity = ColorFactory.CreateRegular(SPD_Data.FromFile(@"F:\3D\spds\Gems\Reflectance\Emerald.3a.txt").Select(i=>(float)i).ToArray())
        };


        private static RegularSPD ComputeIor(float min, float max, float lMin, float lMax,float lDelta)
        {
            var rs = new List<float>();
            var dv = (max - min)/((lMax - lMin)/lDelta);
            for (float f = min, l = lMin; l < lMax; l += lDelta, f += dv)
            {
                rs.Add(l);
                rs.Add(f);
            }

            return new RegularSPD(rs.ToArray(), lMin, lMax, (int)lDelta);
        }
    }
}
